package com.ecloud.hbase.config;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Table;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author Squbi
 * @date 2021/7/23 10:25
 */
@Component
public class HbaseConnFactory {

    @Autowired
    Configuration hbaseConfig;

    private Connection conn;

    public Connection getConn() throws IOException {
        if (conn == null || conn.isClosed()) {
            conn = ConnectionFactory.createConnection(hbaseConfig);
        }
        return conn;
    }

    public Admin getAdmin() throws IOException {
        return getConn().getAdmin();
    }

    public Table getTable(String tableName) throws IOException {
        return getTable(TableName.valueOf(tableName));
    }

    public Table getTable(TableName tableName) throws IOException {
        return getConn().getTable(tableName);
    }
}
